//
//  UWFriendDetail.m
//  Urban Words
//
//  Created by sebastian on 2011-11-28.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "UWFriendManager.h"
#import "UWFriendDetail.h"
#import "UWSettingsController.h"
#import "UILabel+UW.h"
#import <QuartzCore/CALayer.h>
#import "UWApi.h"
#import "UWHistoryController.h"

@implementation UWFriendDetail
@synthesize friend, picture, gamesWon, gameScore, wordScore;

- (id)initWithFriend:(UWFriend *)fr {
    self = [super initWithFrame:CGRectMake(0, 0, 320, 102)];
    if (self != nil) {
        self.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];

        friend = fr;
        
        picture = [[UIImageView alloc] initWithFrame:CGRectMake(7, 4, 94, 94)];
        picture.layer.cornerRadius = 10.0;
        picture.layer.masksToBounds = YES;
        [self addSubview:picture];
        
        UILabel *gamesWonText = [[UILabel alloc] initWithFrame:CGRectMake(108, 6, 101, 21)];
        gamesWonText.text = @"Games Won";
        gamesWonText.font = [UIFont systemFontOfSize:15.0];
        gamesWonText.backgroundColor = [UIColor clearColor];
        [self addSubview:gamesWonText];
        [gamesWonText release];
        
        UILabel *gameScoreText = [[UILabel alloc] initWithFrame:CGRectMake(108, 43, 135, 21)];
        gameScoreText.text = @"Highest Game Score";
        gameScoreText.font = [UIFont systemFontOfSize:14.0];
        gameScoreText.backgroundColor = [UIColor clearColor];
        [self addSubview:gameScoreText];
        [gameScoreText release];
        
        UILabel *wordScoreText = [[UILabel alloc] initWithFrame:CGRectMake(108, 63, 135, 21)];
        wordScoreText.text = @"Highest Word Score";
        wordScoreText.font = [UIFont systemFontOfSize:14.0]; 
        wordScoreText.backgroundColor = [UIColor clearColor];
        [self addSubview:wordScoreText];
        [wordScoreText release];
        
        gamesWon = [[UILabel alloc] initWithFrame:CGRectMake(226, 6, 78, 21)];
        gamesWon.text = @"0";
        gamesWon.font = [UIFont systemFontOfSize:15.0];
        gamesWon.backgroundColor = [UIColor clearColor];
        gamesWon.textAlignment = UITextAlignmentRight;
        [self addSubview:gamesWon];
        
        gameScore = [[UILabel alloc] initWithFrame:CGRectMake(251, 43, 53, 21)];
        gameScore.text = @"0p";
        gameScore.font = [UIFont systemFontOfSize:14.0]; 
        gameScore.backgroundColor = [UIColor clearColor];
        gameScore.textAlignment = UITextAlignmentRight;
        [self addSubview:gameScore];
        
        wordScore = [[UILabel alloc] initWithFrame:CGRectMake(251, 63, 53, 21)];
        wordScore.text = @"0p";
        wordScore.font = [UIFont systemFontOfSize:14.0];
        wordScore.backgroundColor = [UIColor clearColor];
        wordScore.textAlignment = UITextAlignmentRight;
        [self addSubview:wordScore];
    }
    //[self performSelectorInBackground:@selector(updateInfo) withObject:nil];
    //[self updateInfo];
    return self;
}

- (void)viewDidAppear {
    // Get highscore info from server
    NSLog(@"Friend detail Api call");
    [UWApi read:[NSString stringWithFormat:@"highscore/%@",friend.fid] listener:self action:@selector(gotHighscore:)];
}

- (void)gotHighscore:(NSData *)data {
    [[UWFriendManager sharedManager] parseDataForHighscore:data delegate:self];
}

- (void)updateInfo {
    NSLog(@"Update info Friend detail");
    // reload friend
    friend = [[UWFriendManager sharedManager] getFriend:friend.fid];
    // set highscores
    gamesWon.text = friend.gameswon;
    gameScore.text = [NSString stringWithFormat:@"%@p", friend.gamehighscore];
    wordScore.text = [NSString stringWithFormat:@"%@p", friend.wordhighscore];
    NSLog(@"Got friend");
    // Image
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@-large.png", docDir, friend.fid];
    NSLog(@"%@", pngFilePath);
    if ([[NSFileManager defaultManager] fileExistsAtPath:pngFilePath]) {
        picture.image = [UIImage imageWithContentsOfFile:pngFilePath];
    } else {
        picture.image = [[UWFriendManager sharedManager] getLargeImage:friend];
    }    
    NSLog(@"Picture");
}

#pragma mark - View lifecycle
/*
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // Name in navBar
    UIView *labelContainer = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 30)];
    labelContainer.backgroundColor = [UIColor clearColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 6, 200, 30)];
    [label setSpecialText:friend.name];
    [labelContainer addSubview:label];
    //self.navigationItem.titleView = labelContainer;
    [label release];
    
    picture.layer.cornerRadius = 10.0;
    picture.layer.masksToBounds = YES;
    
    //Background image
    backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"uw-gamesscreen-background"]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
    
    // highscore
    gamesWon.text = friend.gameswon;
    gameScore.text = [NSString stringWithFormat:@"%@p", friend.gamehighscore];
    wordScore.text = [NSString stringWithFormat:@"%@p", friend.wordhighscore];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {    
    // Get highscore info from server
    NSLog(@"Api call");
    [UWApi call:[NSString stringWithFormat:@"highscore/%@",friend.fid] delegate:self];
    // Image
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@-large.png", docDir, friend.fid];
    if ([[NSFileManager defaultManager] fileExistsAtPath:pngFilePath]) {
        picture.image = [UIImage imageWithContentsOfFile:pngFilePath];
    } else {
        picture.image = [[UWFriendManager sharedManager] getLargeImage:friend];
    }    

}
 */
/*
- (void)viewDidUnload
{
    [self setPicture:nil];
    [self setFriend:nil];
    [self setGamesWon:nil];
    [self setGameScore:nil];
    [self setWordScore:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [picture release];
    [friend release];
    [gamesWon release];
    [gameScore release];
    [wordScore release];
    [super dealloc];
}
@end
